
package WebSearch;

import java.net.*;
import java.util.ArrayList;
import java.util.List;
import java.io.*;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import CommonClass.IMG;
import CommonClass.URLlink;
import CommonClass.keyword;

public class WebSearch {
	
	private String getURL_Page(String URLStr) throws Exception
	{		
        URL anyURL = new URL(URLStr);
        URLConnection yc = anyURL.openConnection();
        BufferedReader in = new BufferedReader(
                                new InputStreamReader(
                                yc.getInputStream()));
        String inputLine;
        String WebPage = new String();
        while ((inputLine = in.readLine()) != null)
        { 
            //System.out.println(inputLine);
            WebPage += inputLine;
        }
        in.close();
        
		return WebPage;	
	}
	
	private String getFile(String FileStr) throws IOException
	{
		FileReader fr = new FileReader(FileStr);
		BufferedReader textReader = new BufferedReader(fr);
		
		String textData = new String();
		String aLine = new String();
		while((aLine = textReader.readLine()) != null)
		{
			textData += aLine;
		}
		textReader.close();
		return textData;
	}
	
	private boolean checkURL()
	{
		
		return false;
	}
	public ArrayList<URLlink> getHREFs(String URL) throws Exception
	{
		
		String test;
		ArrayList<URLlink> listOfURL = new ArrayList<URLlink>();
		test = getURL_Page(URL);
		//File input = new File("C:\\Project\\index1.htm");
		//test = ReadFile("C:\\Users\\Jasmond\\Documents\\NUS\\SEM 1 13_14\\CG3204L\\Project\\index1.htm");
		Document doc = Jsoup.parse(test);//input, "UTF-8", "http://example.com/");
		
		Element ele = doc.body();
		Elements ahref = ele.getElementsByTag("a");
		//Elements img = ele.getElementsByTag("img");
		for (Element link : ahref) 
		{
			  ArrayList<keyword> keywords;
			  String linkHref = link.attr("href");
			  String linkText = link.text();
			  String linkAlt = link.attr("alt");
			  String linkTitle = link.attr("title");
			  if(!linkHref.isEmpty())
			  {
				 if(!linkText.isEmpty() || !linkAlt.isEmpty() || !linkTitle.isEmpty())
				 {
					 keywords = new ArrayList<keyword>();
					 if(!linkText.isEmpty())
					 {
						 keywords.add(new keyword(linkHref, "TEXT", linkText));
					 }
					 if(!linkAlt.isEmpty())
					 {
						 keywords.add(new keyword(linkHref, "ALT", linkAlt));
					 }
					 if(!linkTitle.isEmpty())
					 {
						 keywords.add(new keyword(linkHref, "TITLE", linkTitle));
					 }
				 }
				 else
				 {
					 keywords = null;
				 }
				 listOfURL.add(new URLlink(URL, linkHref, keywords));
				 //System.out.println(linkHref + " \t => "+ linkText);
			 }
		}
		if(listOfURL.size() == 0)
		{
			listOfURL.clear();
			return null;
		}
		
		return listOfURL;
	}
	
	public ArrayList<IMG> getIMGs(String URL) throws Exception
	{
		
		String test;
		ArrayList<IMG> listOfURL = new ArrayList<IMG>();
		test = getURL_Page(URL);
		//File input = new File("C:\\Project\\index1.htm");
		//test = ReadFile("C:\\Users\\Jasmond\\Documents\\NUS\\SEM 1 13_14\\CG3204L\\Project\\index1.htm");
		Document doc = Jsoup.parse(test);//input, "UTF-8", "http://example.com/");
		
		Element ele = doc.body();
		//Elements ahref = ele.getElementsByTag("a");
		Elements img = ele.getElementsByTag("img");
		for (Element link : img) 
		{
			  ArrayList<keyword> keywords;
			  String linkSrc = link.attr("src");
			  String linkAlt = link.attr("alt");
			  String linkTitle = link.attr("title");
			  String height = link.attr("height");		// need to convert to int
			  String width = link.attr("width");		// need to conver to int
			  if(!linkSrc.isEmpty())
			  {
				 if( !linkAlt.isEmpty() || !linkTitle.isEmpty())
				 {
					 keywords = new ArrayList<keyword>();
					 if(!linkAlt.isEmpty())
					 {
						 keywords.add(new keyword(linkSrc, "ALT", linkAlt));
					 }
					 if(!linkTitle.isEmpty())
					 {
						 keywords.add(new keyword(linkSrc, "TITLE", linkTitle));
					 }
				 }
				 else
				 {
					 keywords = null;
				 }
				 //String IMG, int height, int width, ArrayList<keyword> keywords
				 listOfURL.add(new IMG(URL, linkSrc, 0, 0, keywords));
				 //System.out.println(linkHref + " \t => "+ linkText);
			 }
		}
		if(listOfURL.size() == 0)
		{
			listOfURL.clear();
			return null;
		}
		
		return listOfURL;
	}
}
